<template>
  <span>{{ result.num }}{{ result.unit }}</span>
</template>
<script>
export default {
  name:'fileSize',
  props:{
    size:{
      type:Number,
      validator: function (val) {
        return /^[1-9]\d*$/.test(val)
      },
      default:function(){ return 1.44 * 1024 * 1024 },
    },
  },
  computed:{
    result:function(){
      let r = this.units.reduce((t,v,i,a)=>{
        if( t.num < 1024 ){
          return t;//{ num:t.num, unit:t.unit }
        }else{
          if(i == a.length - 1){
            return { num:t.num, unit:v }
          }else{
            return { num:t.num / 1024, unit:a[i+1] }
          }
        }
      },{ num:this.size, unit:this.units[0] });
      return {
        num:r.num.toFixed(2),
        unit:r.unit
      };
    },
  },
  data () {
    return {
      units:['B','KB','MB','GB','TB'],
    }
  },
}
</script>